package request

import (
	"github.com/astaxie/beego/validation"
	"shop-common/utils"
)

type ShippingTemplatesRequest struct {
	Id             uint                              `json:"id"`                                  //主键
	Name           string                            `binding:"required" json:"name"`             //模板名称
	Type           uint8                             `binding:"required" default:"1" json:"type"` //计费方式 Appoint
	RegionList     []*ShippingTemplatesRegionRequest `json:"regionList"`                          //配送区域及运费
	Appoint        uint8                             `default:"1" json:"appoint"`                 //指定包邮
	AppointList    []*ShippingTemplatesFreeRequest   `json:"appointList"`                         //指定包邮设置
	NoDelivery     uint8                             `default:"1" json:"noDelivery"`              //指定不包邮
	NoDeliveryList []*ShippingNoDeliveryRequest      `json:"noDeliveryList"`                      //指定不包邮设置
	Seq            int                               `json:"seq"`                                 //排序
}

func (s *ShippingTemplatesRequest) Valid(v *validation.Validation) {
	if !utils.UintIncludes([]uint{1, 2, 3}, uint(s.Type)) {
		v.SetError("Type", "计费方式不合法")
	} else if len(s.RegionList) < 1 {
		v.SetError("TemplateList", "配送区域及运费不合法")
	}
}

//付费
type ShippingTemplatesRegionRequest struct {
	//CityId       string  `binding:"required" json:"cityId"`     //城市ID, 多个逗号分割。全国 all
	Region       []*RegionRequest `json:"region"`                        //城市信息
	RegionName   string           `binding:"required" json:"regionName"` //城市名称描述
	First        float64          `binding:"required" json:"first"`      //首件
	FirstPrice   float64          `binding:"required" json:"firstPrice"` //首件运费
	Renewal      float64          `json:"renewal"`                       //续件
	RenewalPrice float64          `json:"renewalPrice"`                  //续件运费
	UniqId       string           `json:"uniqId"`                        //分组唯一值
}

func (s *ShippingTemplatesRegionRequest) Valid(v *validation.Validation) {
	if len(s.Region) < 1 {
		v.SetError("Region", "请选择城市")
	} else if s.First < 0.1 {
		v.SetError("First", "首件金额不能低于0.1")
	} else if s.FirstPrice < 0.1 {
		v.SetError("First", "首件运费金额不能低于0.1")
	} else if s.Renewal < 0.1 {
		v.SetError("First", "续件不能低于0.1")
	} else if s.RenewalPrice < 0.1 {
		v.SetError("First", "续件运费金额不能低于0.1")
	}
}

type ShippingTemplatesFreeRequest struct {
	//CityId string  `binding:"required" json:"cityId"` //城市ID, 多个逗号分割。全国 all
	Place     []*RegionRequest `json:"place"`                        //城市信息
	PlaceName string           `binding:"required" json:"placeName"` //城市名称描述
	Number    float64          `binding:"required" json:"number"`    //包邮件数
	Price     float64          `binding:"required" json:"price"`     //包邮金额
}

func (s *ShippingTemplatesFreeRequest) Valid(v *validation.Validation) {
	if len(s.Place) < 1 {
		v.SetError("Region", "请选择城市")
	} else if s.Number < 0.1 {
		v.SetError("Number", "包邮不能低于0.1")
	} else if s.Price < 0 {
		v.SetError("Price", "包邮金额不能低于0")
	}
}

type ShippingNoDeliveryRequest struct {
	Place []*RegionRequest `json:"place"` //城市信息
}

func (s *ShippingNoDeliveryRequest) Valid(v *validation.Validation) {
	if len(s.Place) < 1 {
		v.SetError("Place", "请选择城市")
	}
}

type RegionRequest struct {
	RegionName string                 `json:"regionName"`
	CityId     uint                   `json:"cityId"`
	Children   []*ChildrenCityRequest `json:"children"`
}

type ChildrenCityRequest struct {
	CityId uint   `json:"cityId"`
	Name   string `json:"name"`
}
